package io.github.talelin.latticy.common.util;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.util.List;
import java.util.Map;

/**
 * @ClassName：PdfClient
 * @Author: ljj
 * @Date: 2022/5/23 11:42
 */
public class PdfClient {

    public static void createPdf(PdfWriter writer, Document document, List<Map<String,Object>> mapList,
                                 String sku) throws Exception {
        // 打开文档
        document.open();
        Paragraph paragraph = new Paragraph("xxxxx系统", PdfUtil.setFont(12f, BaseColor.GRAY, false));
        // 居中
        paragraph.setAlignment(Element.ALIGN_CENTER);
        document.add(paragraph);

        PdfPTable table = PdfUtil.setTable(3, 300, 40f, 50f, new float[]{290, 70, 50});

        PdfUtil.setCell(table, BaseColor.BLACK, false, 60, false,
                PdfUtil.setSpace("中远", 3, 1, 15, 0, PdfUtil.setFont(20f, BaseColor.BLACK, false)));

        // 条形码
        PdfUtil.setBarcodeCell(table, 20, 150, 3, 100,
                PdfUtil.createBarcode(writer, sku, true));

        PdfUtil.setCell(table, BaseColor.BLACK, false, 60, false,
                PdfUtil.setSpace("sku:" + sku, 3, 1, 15, 0, PdfUtil.setFont(17f, BaseColor.GRAY, false)));

        Font font = PdfUtil.setFont(16f, BaseColor.BLACK, false);
        PdfUtil.setCell(table, BaseColor.GRAY, false, 60, false,
                PdfUtil.setSpace("商品名称", 1, 1, 10, 0, font));

        PdfUtil.setCell(table, BaseColor.GRAY, true, 60, false,
                PdfUtil.setSpace("商品规格", 1, 1, 0, 0, font));

        PdfUtil.setCell(table, BaseColor.GRAY, true, 60, false,
                PdfUtil.setSpace("件数", 1, 1, 0, 0, font));

       // 插入SKU信息
        mapList.forEach(map -> {
            Font fontSku = PdfUtil.setFont(16f, BaseColor.GRAY, false);
            // 商品名称
            PdfUtil.setCell(table, BaseColor.GRAY, false, 60, false,
                    PdfUtil.setSpace(map.get("name").toString(), 1, 1, 10, 0, fontSku));
        });
        document.add(table);
        // 关闭文档
        document.close();
    }
}
